package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.*;
import com.example.hospital.api.db.pojo.DrugEntity;
import com.example.hospital.api.service.DrugService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/drug")
public class DrugController {
    @Resource
    private DrugService drugService;

    @PostMapping("/searchByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "VIDEO_DIAGNOSE:SELECT"}, mode = SaMode.OR)
    public R searchByPage(@RequestBody @Valid SearchDrugByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = drugService.searchByPage(param);
        return R.ok().put("result", pageUtils);
    }

    @GetMapping("/searchAllDrug")
    @SaCheckLogin
    public R searchDeptAndSub() {
        HashMap map = drugService.searchAllDrug();
        return R.ok().put("result", map);
    }

    @PostMapping("/insert")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "VIDEO_DIAGNOSE:SELECT"}, mode = SaMode.OR)
    public R insert(@RequestBody @Valid InsertDrugInfForm form) {
        Map param = BeanUtil.beanToMap(form);
        drugService.insert(param);
        return R.ok();
    }

    @GetMapping("/searchAll")
    @SaCheckLogin
    public R searchAll() {
        HashMap map = drugService.searchAll();
        return R.ok().put("result", map);
    }

    @PostMapping("/searchById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "VIDEO_DIAGNOSE:SELECT"}, mode = SaMode.OR)
    public R searchById(@RequestBody @Valid SearchDrugByIdForm form) {
        HashMap map = drugService.searchById(form.getId());
        return R.ok(map);
    }

    @PostMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "VIDEO_DIAGNOSE:SELECT"}, mode = SaMode.OR)
    public R update(@RequestBody @Valid UpdateDrugForm form) {
        Map param = BeanUtil.beanToMap(form);
        drugService.update(param);
        return R.ok();
    }

    @PostMapping("/deleteByIds")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "VIDEO_DIAGNOSE:SELECT"}, mode = SaMode.OR)
    public R deleteByIds(@RequestBody @Valid DeleteDrugByIdsForm form) {
        drugService.deleteByIds(form.getIds());
        return R.ok();
    }
}
